import 'dart:convert';
import 'package:demo/pages/total/total.dart';
import 'package:demo/util/asset.dart';
import 'package:flutter/material.dart';

class WalletDetailCom extends StatefulWidget {
  @override
  _WalletDetailComState createState() => _WalletDetailComState();
}

class _WalletDetailComState extends State<WalletDetailCom> {
  var yearSumSpend,
      yearSumIncome,
      monthSumSpend,
      monthSumIncome,
      daySumSpend,
      daySumIncome;
  // 初始化数据
  void initData() {
    var myData = jsonDecode(SpUtil.preferences.getString('myData'));
    var today = DateTime.now();
    var year = today.year;
    var month = today.month;
    var day = today.day;

    for (var item in myData) {
      if (year == item['year']) {
        yearSumSpend = item['sumSpend'];
        yearSumIncome = item['sumIncome'];
        SpUtil.preferences.setString('thisYear', jsonEncode(item));
        for (var monthItem in item['monthList']) {
          if (month == monthItem['month']) {
            monthSumSpend = monthItem['sumSpend'];
            monthSumIncome = monthItem['sumIncome'];
            SpUtil.preferences.setString('thisMonth', jsonEncode(monthItem));
            for (var dayItem in monthItem['dayList']) {
              print(day);
              print(dayItem['time']);
              if (day == dayItem['time']) {
                daySumSpend = dayItem['sumSpend'];
                daySumIncome = dayItem['sumIncome'];
                SpUtil.preferences.setString('thisDay', jsonEncode(dayItem));
                break;
              } else {
                daySumSpend = 0;
                daySumIncome = 0;
                SpUtil.preferences.setString('thisDay', jsonEncode([]));
              }
            }
            break;
          } else {
            SpUtil.preferences.setString('thisMonth', jsonEncode([]));
          }
        }
        break;
      } else {
        SpUtil.preferences.setString('thisYear', jsonEncode([]));
      }
    }
  }

  InkWell detailItem(text, sumSpend, sumIncome) {
    return InkWell(
      onTap: () {
        SpUtil.preferences.setString('totalType', text);
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
          return TotalPageWidget();
        }));
      },
      child: Container(
        height: 80.0,
        margin: EdgeInsets.all(10.0),
        padding: EdgeInsets.only(right: 20.0),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(color: Colors.black45, width: 1.0),
          ),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  text,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 18.0,
                  ),
                ),
                Text(
                  '$sumSpend元',
                  style: TextStyle(
                    fontSize: 16.0,
                    fontWeight: FontWeight.bold,
                    color: Colors.red[400],
                  ),
                )
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text('收入'),
                Text(
                  '$sumIncome元',
                  style: TextStyle(
                    fontSize: 16.0,
                    fontWeight: FontWeight.bold,
                    color: Colors.green[400],
                  ),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    initData();
    return Column(
      children: [
        detailItem('今天', daySumSpend, daySumIncome),
        detailItem('本月', monthSumSpend, monthSumIncome),
        detailItem('本年', yearSumSpend, yearSumIncome),
      ],
    );
  }
}
